package flatten114;


import java.util.ArrayList;
import java.util.List;

public class Flatten {

    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        preorderReversa(root,list);

        if (list.size()==0) {
            return;
        }
        TreeNode temp = list.get(0);

        for (int i = 1; i < list.size(); i++) {
            temp.left = null;
            temp.right = list.get(i);
            temp = temp.right;
        }
    }


    public void preorderReversa(TreeNode root, List<TreeNode> list) {

        if (root == null) {
            return;
        }

        list.add(root);
        preorderReversa(root.left,list);
        preorderReversa(root.right,list);
    }



}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}